<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\admin\model\StoreJoinActivity;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Activity extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Activity();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['activity.is_delete','=',0];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        $count = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['activity_start_time']  = date('Y-m-d H:i',$value['activity_start_time']);
            $list[$key]['activity_end_time']  = date('Y-m-d H:i',$value['activity_end_time']);
            $list[$key]['multiLensList'] = json_decode($value['multiLensList']);
        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function consumerLog(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['activity_consumer.is_delete','=',0];
        if ($this->request->platform_id >0 ){
            $where[] = ['activity_consumer.platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['activity_name']) &&   $param['activity_name']) {
            $where[] = ['activityInfo.name', 'like', "%{$param['activity_name']}%"];
        }
        $ActivityConsumerModel = new ActivityConsumer();
        $count = $ActivityConsumerModel
            ->withJoin(['platformInfo','consumerInfo','activityInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $ActivityConsumerModel->withJoin(['platformInfo','consumerInfo','activityInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['start_time']  = date('Y-m-d H:i',$value['start_time']);
            $list[$key]['end_time']  = date('Y-m-d H:i',$value['end_time']);
        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function add(){
        $request = $this->request;
        $param = $request->param();
        $date1 = new \DateTime($param['activity_start_time']);
        $date2 = new \DateTime($param['activity_end_time']);
        $interval = $date1->diff($date2);
        $days = $interval->days;
        if (empty($param['activity_start_time'])){
            return  msg([], 1, "请输入活动开始时间") ;
        }
        if (empty($param['activity_end_time'])){
            return  msg([], 1, "请输入活动结束时间") ;
        }
        if (empty($param['name'])){
            return  msg([], 1, "请输入活动名称") ;
        }
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'sponsor_name' => $param['sponsor_name'],
            'sponsor_telphone' => $param['sponsor_telphone'],
            'activity_start_time' => strtotime($param['activity_start_time']),
            'activity_end_time' => strtotime($param['activity_end_time']) + 86399,
            'total_claim_number' => $param['total_claim_number'],
            'invitation_obtain_frequency' =>  $param['invitation_obtain_frequency'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'multiLensList' => json_encode($param['multiLensList']),
          //  'activity_img' => $param['activity_img'],
            'goods_img' => $param['goods_img'],
        ];
        if($param['latitude'] != '39.89945'){
            $data['latitude']  = $param['latitude'];
        }
        if($param['longitude'] != '116.40969'){
            $data['longitude']  = $param['longitude'];
        }
        $this->model->save($data);
        if ($param['multiLensList']){
            foreach ($param['multiLensList'] as $value){
                if (empty($value['consumer_id'])){
                    return  msg([], 1, "请添加消费券") ;
                }
            }
        }
        for ($i =0 ;$i<= $days;$i++){
            if ($param['multiLensList']){
                foreach ($param['multiLensList'] as $value){
                    $ActivityConsumer = [
                        'activity_id' => $this->model->id,
                        'consumer_id'=>$value['consumer_id'],
                        'claim_number' => $value['claim_number'],
                        'platform_id' =>$this->request->platform_id,
                        'start_time' =>strtotime (date("Y-m-d", strtotime("+$i days", $data['activity_start_time'])).' '.$value['start_time']),
                        'end_time' =>strtotime(date("Y-m-d", strtotime("+$i days", $data['activity_start_time'])).' '.$value['end_time']),
                        'surplus_number' =>  $value['claim_number'],
                    ];
                    $ActivityConsumerModel = new ActivityConsumer();
                    $ActivityConsumerModel->save($ActivityConsumer);
                    }
            }

        }
        return  msg([], 0, "保存成功") ;
    }

    public function edit(){

        $request = $this->request;
        $param = $request->param();
        $date1 = new \DateTime($param['activity_start_time']);
        $date2 = new \DateTime($param['activity_end_time']);
        $interval = $date1->diff($date2);
        $days = $interval->days;
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'sponsor_name' => $param['sponsor_name'],
            'sponsor_telphone' => $param['sponsor_telphone'],
            'activity_start_time' => strtotime($param['activity_start_time']),
            'activity_end_time' => strtotime($param['activity_end_time']) + 86399,
            'total_claim_number' => $param['total_claim_number'],
            'invitation_obtain_frequency' =>  $param['invitation_obtain_frequency'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'multiLensList' => json_encode($param['multiLensList']),
          //  'activity_img' => $param['activity_img'],
            'goods_img' => $param['goods_img'],
        ];
        if($param['latitude'] != '39.89945'){
            $data['latitude']  = $param['latitude'];
        }
        if($param['longitude'] != '116.40969'){
            $data['longitude']  = $param['longitude'];
        }
        $info = $this->model->where(['id' => $param['id']])->find();

        if ($info['status'] == 2){
            if (json_encode($param['multiLensList']) !=$info['multiLensList'] ){
                return msg([], 1, '活动已开始消费券不允许调整');
            }

        }


        $this->model->where(['id' => $param['id']])->update($data);
        $ActivityConsumerModel = new ActivityConsumer();
        $ActivityConsumerModel->where(['activity_id'=>$param['id'] ])->update(['is_delete' => 1]);

        for ($i =0 ;$i<= $days;$i++){
            foreach ($param['multiLensList'] as $value){
                $ActivityConsumer = [
                    'activity_id' => $param['id'],
                    'consumer_id'=>$value['consumer_id'],
                    'claim_number' => $value['claim_number'],
                    'platform_id' =>$this->request->platform_id,
                    'start_time' =>strtotime (date("Y-m-d", strtotime("+$i days", $data['activity_start_time'])).' '.$value['start_time']),
                    'end_time' =>strtotime(date("Y-m-d", strtotime("+$i days", $data['activity_start_time'])).' '.$value['end_time']),
                    'surplus_number' =>  $value['claim_number'],
                ];

                $ActivityConsumerModel = new ActivityConsumer();
                $ActivityConsumerModel->save($ActivityConsumer);
            }
        }
        return  msg([], 0, "保存成功") ;
    }

    public function consumerLogRemove(){
        $request = $this->request;
        $param = $request->param();
        $ActivityConsumerModel = new ActivityConsumer();

        $ActivityConsumerModel->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([

        ], 0, "删除成功");
    }

    public function delete(){
        $request = $this->request;
        $param = $request->param();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        $ActivityConsumerModel = new ActivityConsumer();

        $ActivityConsumerModel->whereIn('activity_id', $param['id'])->update(['is_delete' => 1]);

        return msg([

        ], 0, "删除成功");
    }

    public function joinlog(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where =[] ;
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        $StoreJoinActivityModel  = new StoreJoinActivity();
        $count = $StoreJoinActivityModel
            ->withJoin(['platformInfo','storeInfo','activityInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $StoreJoinActivityModel
            ->withJoin(['platformInfo','storeInfo','activityInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['activityInfo']['activity_start_time']  = date('Y-m-d H:i',$value['activityInfo']['activity_start_time']);
            $list[$key]['activityInfo']['activity_end_time']  = date('Y-m-d H:i',$value['activityInfo']['activity_end_time']);
        }

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }


    public function start_activity(){
        $request = $this->request;
        $param = $request->param();
        $StoreJoinActivityModel  = new StoreJoinActivity();
        $count  = $StoreJoinActivityModel


            ->where(['activity_id'=> $param['id'],'status' => 2 ])->count();
        if ($count  ==0){
            return  msg([], 1, "暂无商家参与") ;
        }

        $this->model->where(['id' => $param['id']])->update(['status' => 2]);
        return  msg([], 0, "操作成功") ;
    }


    public function joinStatus(){
        $request = $this->request;
        $param = $request->param();
        $StoreJoinActivityModel  = new StoreJoinActivity();
        $StoreJoinActivityInfo = $StoreJoinActivityModel->where(['id' =>$param['id'] ])->find();
        $StoreJoinActivityModel->where(['id' =>$param['id'] ])->update([$param['field'] => $param['value']]);


        if ( $param['value'] == 2 ){
            $ActivityModel  =  new \app\admin\model\Activity();

            $ActivityModel->where(['id' => $StoreJoinActivityInfo['activity_id']])->inc('store_join_count')->update();
        }

        return  msg([], 0, "操作成功") ;
    }







}